package com.bt.service.impl;

import com.bt.constant.MessageConstant;
import com.bt.context.BaseContext;
import com.bt.dto.SysUserLoginDTO;
import com.bt.entity.*;
import com.bt.exception.AccountNotFoundException;
import com.bt.exception.PasswordErrorException;
import com.bt.mapper.*;
import com.bt.service.IndexService;
import com.bt.utils.MD5Utils;
import com.bt.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author 青
 * @Date 2023-04-28 22:19
 */
@Slf4j
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    YddCateMapper yddCateMapper;
    @Autowired
    YddAdvposMapper yddAdvposMapper;
    @Autowired
    YddTagMapper yddTagMapper;
    @Autowired
    YddAdvimgMapper yddAdvimgMapper;
    @Autowired
    YddUserMapper yddUserMapper;
    @Autowired
    YddArticleMapper yddArticleMapper;
    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    SysDictMapper sysDictMapper;
    @Autowired
    SysRoleMapper sysRoleMapper;


    /**
     * 用户登录
     *
     * @param sysUserLoginDTO
     * @return
     */
    @Override
    public SysUser login(SysUserLoginDTO sysUserLoginDTO) {
        String loginName = sysUserLoginDTO.getLogin_name();
        String pass = sysUserLoginDTO.getPassword();

        SysUser sysUser = sysUserMapper.getByUserName(loginName);

        if (sysUser == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        String password = MD5Utils.code(pass);
        log.info("密码: {}", password);
        if (!password.equals(sysUser.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        return sysUser;
    }

    /**
     * 主页统计信息
     *
     * @return
     */
    @Override
    public BaseInfoVO baseInfo() {
        //文章总数
        List<YddCateVO> yddCateVOList = yddCateMapper.countCateArticleNum();

        //广告位总数
        Integer advposNum = yddAdvposMapper.countAdvposNum();

        //标签总数
        Integer yddTagNum = yddTagMapper.countTagNum();

        //管理员总数
        Integer adminNum = sysUserMapper.countAdminNum();

        //文章总数
        Integer articleNum = yddCateMapper.countArticleNum();

        //分类总数
        Integer cateNum = yddCateMapper.countCateNum();

        //广告图总数
        Integer advimgNum = yddAdvimgMapper.countAdvimgNum();

        //最新用户数据(6条)
        List<YddUser> yddUserList = yddUserMapper.countNewUser();

        //热门文章数据(前6条)
        List<YddArticle> hotArticle = yddArticleMapper.countHotArticle();
        //用户数据
        UserCountVo user = new UserCountVo();
        Integer userTotal = yddUserMapper.countUser();
        Integer manNum = yddUserMapper.countManNum();
        Integer womanNum = yddUserMapper.countWomanNum();
        user.setTotal(userTotal);
        user.setUser_man(manNum);
        user.setUser_woman(womanNum);


        BaseInfoVO baseInfoVO = new BaseInfoVO();
        baseInfoVO.setAll_cate(yddCateVOList);
        baseInfoVO.setAdvpos_num(advposNum);
        baseInfoVO.setTag_num(yddTagNum);
        baseInfoVO.setAdmin_num(adminNum);
        baseInfoVO.setArticle_num(articleNum);
        baseInfoVO.setCate_num(cateNum);
        baseInfoVO.setAdvimg_num(advimgNum);
        baseInfoVO.setNew_user(yddUserList);
        baseInfoVO.setHot_article(hotArticle);
        baseInfoVO.setUser(user);

        return baseInfoVO;


    }

    /**
     * 获取用户资料
     *
     * @return
     */
    @Override
    public ProfileVO getUserData() {
        //封装userInfo
        String id = BaseContext.getCurrentId();
        log.info("用户id为: {}", id);
        SysUser sysUser = sysUserMapper.findById(id);

        String roleId = sysUser.getRoleId();
        String roleIds = roleId.substring(2, roleId.length() - 2);

        String roleName = sysRoleMapper.getRoleName(roleIds);

        SysUserVO userInfo = new SysUserVO();
        BeanUtils.copyProperties(sysUser, userInfo);
        userInfo.setRolename(roleName);

        //封装menuList
        List<SysMenu> sysMenuList = sysMenuMapper.selectSysMenu();

        //封装dictsList;
        SysDictVO sysDictVO = new SysDictVO();
        List<SysDict> sysDicts = sysDictMapper.getAllSysDicts();
        // 将查询结果封装到SysDictVO对象中
        Map<String, String> interfaceTypeMap = new LinkedHashMap<>();
        Map<String, String> orderTypeMap = new LinkedHashMap<>();
        Map<String, String> reportTypeMap = new LinkedHashMap<>();
        Map<String, String> tableTypeMap = new LinkedHashMap<>();
        Map<String, String> themeMap = new LinkedHashMap<>();
        for (SysDict sysDict : sysDicts) {
            switch (sysDict.getType()) {
                case "interface_type":
                    interfaceTypeMap.put(sysDict.getValue(), sysDict.getLabel());
                    break;
                case "order_type":
                    orderTypeMap.put(sysDict.getValue(), sysDict.getLabel());
                    break;
                case "report_type":
                    reportTypeMap.put(sysDict.getValue(), sysDict.getLabel());
                    break;
                case "table_type":
                    tableTypeMap.put(sysDict.getValue(), sysDict.getLabel());
                    break;
                case "theme":
                    themeMap.put(sysDict.getValue(), sysDict.getLabel());
                    break;
                default:
                    break;
            }

            sysDictVO.setOrder_type(orderTypeMap);
            sysDictVO.setReport_type(reportTypeMap);
            sysDictVO.setTable_type(tableTypeMap);
            sysDictVO.setTheme(themeMap);

        }

        //封装ProfileVO
        ProfileVO profileVO = new ProfileVO();
        profileVO.setUserInfo(userInfo);
        profileVO.setMenuList(sysMenuList);
        profileVO.setDictsList(sysDictVO);
        return profileVO;
    }
}
